//
//  ViewController.m
//  10802pickerViewDemo
//
//  Created by student on 16/2/23.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

NSInteger n1;
NSInteger n2;

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property(strong,nonatomic)NSArray *leftList;
@property(strong,nonatomic)NSArray *rightList;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *btn;

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.leftList=@[@"火鸡",@"花生酱",@"金枪鱼沙拉",@"鸡丁沙拉",@"烤牛肉",@"蔬菜"];
    self.rightList=@[@"白面包",@"粗面粉面包",@"黑麦面包",@"酸面包",@"天然有机7谷"];
  
                    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btnPressed:(UIButton*)sender {
    int sel1=[self.pickerView selectedRowInComponent:0];
    int sel2=[self.pickerView selectedRowInComponent:1];
    
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"您选择了%@和%@",self.leftList[sel1],self.rightList[sel2]] delegate:self cancelButtonTitle:@"重新选择" otherButtonTitles:@"确定", nil];
    
    [alertView show];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0) {
        return self.leftList.count;
       
    }
    else{
        return self.rightList.count;
    }
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==0) {
        return self.leftList[row];
    }
    else{
        NSLog([NSString stringWithFormat:@"%li,%li",n1,n2]);
        return self.rightList[row];
        
    }
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(component == 0)
    {
        n1 = row;
    }
    else
    {
        n2 = row;
    }
    
          [self.btn setTitle:[NSString stringWithFormat:@"选择%@和%@",self.leftList[n1],self.rightList[n2]] forState:UIControlStateNormal];
  
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self.label.text=@"1";
    if (buttonIndex==alertView.cancelButtonIndex) {
        self.label.text=@"正在选择中";
    }
    else{
           self.label.text=[NSString stringWithFormat:@"我的早饭是%@和%@",self.leftList[n1],self.rightList[n2]];
        
    }
}

@end
